import sys
from bs4 import BeautifulSoup
from selenium import webdriver
import time, datetime
import pymysql
import traceback
import re
from selenium.webdriver.chrome.options import Options
from config import *

url = "http://bmfw.www.gov.cn/yqfxdjcx/risk.html"
# 有头模式
# driver = webdriver.Chrome()
# driver.get(url)
# 无头模式
co = Options()
co.add_argument('--headless')  # 隐藏浏览器
co.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=co)
driver.get(url)

bottomJs = "var q=document.documentElement.scrollTop=100000"  # 底部
topJs = "window.scrollTo(0,0)"  # 顶部


def isElementExist(driver, element):
    flag = True
    ele = driver.find_elements_by_css_selector(element)
    if len(ele) == 0:
        flag = False
        return flag
    if len(ele) == 1:
        return flag
    else:
        flag = False
    return flag


tableElement = "body > div.content > div.container > div:nth-child(2) > div.risk-tables > div:nth-child(1)"
flag = isElementExist(driver, tableElement)
while True:
    if flag:
        print(f"{time.asctime()}页面元素加载成功")
        break
    else:
        print(f"{time.asctime()}页面元素还没加载到,开始刷新,等待10s")
        driver.refresh()
        time.sleep(10)
        flag = isElementExist(driver, tableElement)

h = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[1]")  # 定位高风险选项
m = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[2]")  # 定位中风险选项
t = driver.find_element_by_css_selector(
    "body > div.content > div.container > div:nth-child(2) > div.tabs-header > p").text
pun = "，。……——“”‘’！；"
punList = list(set(list(pun)))
pattern = re.compile("|".join(punList))
utime = re.sub(pattern, "", t[2:12])


# print(utime)


def get_conn():
    """
    :return: 连接，游标
    """
    # 创建连接
    conn = pymysql.connect(host=HOST,
                           user=USER,
                           password=PASSWORD,
                           db=DATABASE,
                           charset="utf8")
    # 创建游标
    cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
    return conn, cursor


def close_conn(conn, cursor):
    cursor.close()
    conn.close()


def get_list(utime, res, driver):
    risk = []
    while True:
        for i in res:
            communitys_flag = i.text.split()
            communitys = communitys_flag[0]
            type = communitys_flag[1]
            fa = i.find_parent("div")  # 寻找父节点
            f = fa.find("div", class_="risk-info-table-title")
            area_flag = f.text.split()
            province = area_flag[0]
            city = area_flag[1]
            county = area_flag[2]
            risk.append([utime, province, city, county, communitys, type])
        driver.execute_script("var q=document.documentElement.scrollTop=100000")
        time.sleep(3)
        next_btn = driver.find_element_by_css_selector("#nextPage")  # 获取下一页按钮的位置
        next_btn_flag = next_btn.is_enabled()
        # print(next_btn_flag)
        if not next_btn_flag:  # 判断下一页按钮是否可以点击
            break
        else:
            # 获取点击下一页
            driver.execute_script("arguments[0].click();", next_btn)
            soup = BeautifulSoup(driver.page_source, "html.parser")
            res = soup.find_all("tr")
    return risk


def get_rish_h(utime, driver, h):
    driver.execute_script("window.scrollTo(0,0)")
    time.sleep(2)
    # h = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[1]")  # 定位高风险选项
    h.click()  # 点击该选项
    time.sleep(2)
    soup = BeautifulSoup(driver.page_source, "html.parser")  # 根据selenium获取soup
    res = soup.find_all("tr")  # 定位到具体地址的tr内容
    risk_h = get_list(utime, res, driver)
    print(f"{time.asctime()}高风险区域详情爬取完毕")
    print(risk_h)
    # driver.close()
    # driver.quit()
    return risk_h


def get_rish_m(utime, driver, m):
    driver.execute_script("window.scrollTo(0,0)")
    time.sleep(2)
    # m = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[2]")  # 定位中风险选项
    m.click()  # 点击该选项
    time.sleep(2)
    soup = BeautifulSoup(driver.page_source, "html.parser")  # 点击后页面更新，soup也需要更新
    res = soup.find_all("tr")
    risk_m = get_list(utime, res, driver)
    print(f"{time.asctime()}中风险区域详情爬取完毕")
    # driver.close()
    # driver.quit()
    print(risk_m)
    return risk_m


def get_rish_l(utime, driver):
    time.sleep(2)
    driver.execute_script("window.scrollTo(0,800);")  # 滚动条拉到顶部
    l = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[3]")  # 定位低风险选项
    l.click()  # 点击该选项
    time.sleep(2)
    soup = BeautifulSoup(driver.page_source, "html.parser")  # 点击后页面更新，soup也需要更新
    res = soup.find_all("tr")
    risk_l = get_list(utime, res, driver)
    print(f"{time.asctime()}低风险区域详情爬取完毕")
    # driver.close()
    return risk_l


def update_risk(utime):
    # 更新 risk_area 表
    # :return:
    # """
    cursor = None
    conn = None
    try:

        conn, cursor = get_conn()
        sql = "insert into risk_area(end_update_time,province,city,county,address,type) values(%s,%s,%s,%s,%s,%s)"
        sql_query = 'select %s=(select end_update_time from risk_area order by id desc limit 1)'  # 对比当前最大时间戳
        cursor.execute(sql_query, utime)  # 传入最新时间戳
        if not cursor.fetchone()[0]:
            print(f"{time.asctime()}开始更新最新数据")
            for item in get_rish_h(utime, driver, h):
                cursor.execute(sql, item)
            for item in get_rish_m(utime, driver, m):
                cursor.execute(sql, item)
            # for item in get_rish_l(utime, driver):
            #     cursor.execute(sql, item)
            conn.commit()  # 提交事务 update delete insert操作
            print(f"{time.asctime()}更新最新数据完毕")
        else:
            print(f"{time.asctime()}已是最新数据！")
    except:
        traceback.print_exc()
    finally:
        close_conn(conn, cursor)
        driver.quit()


if __name__ == "__main__":
    update_risk(utime)

    # s = """请输入参数:
    # up_risk  更新中高风险地区信息
    # """
    # l = len(sys.argv)
    # if l == 1:
    #     print(s)
    # else:
    #     order = sys.argv[1]
    #     if order == "up_risk":
    #         update_risk(utime)
    #     else:
    #         print("错误的参数")
    #         print(s)
